﻿Public Class xtf_ABMisometrico

    Dim ulg_row As DataRow
    Dim obj_isometrico_ As cls_isometrico_ = Nothing
    Dim obj_obra As cls_centrocostoorden_
    Dim obj_linea As cls_linea_

    Public Sub New(ByVal obra As cls_centrocostoorden_, ByVal linea As cls_linea_)
        InitializeComponent()
        obj_obra = obra
        obj_linea = linea
        txt_linea.Text = linea.ToString
        Cargarproveedor_()
        Cargarplano_()
        Cargararea_()
        Cargarskid_()
        Cargarlineaopcional1_()
        Cargarlineaopcional2_()
        AgregarValidaciones()
    End Sub

    Public Overrides Sub CargarObjetos()
        Cargarproveedor_()
        Cargarplano_()
        Cargararea_()
        Cargarskid_()
    End Sub
    Private Sub Cargarlineaopcional1_()
        Dim tmp_obj As Integer = 0
        tmp_obj = cbo_idlinea1.EditValue

        Dim linea_ As New cls_linea_
        linea_.CargarXidobra(obj_obra.idcentrocostoorden)
        For Each linea As cls_linea_ In linea_.Items
            If linea.idlinea = obj_linea.idlinea Then
                linea_.Items.Remove(linea)
                Exit For
            End If
        Next
        If linea_.Items.Count = 0 Then linea_.Items = Nothing
        cargarCombo(cbo_idlinea1, linea_.Items, False, True)
        cbo_idlinea1.EditValue = IIf(tmp_obj <> 0, tmp_obj, cbo_idlinea1.EditValue)
    End Sub
    Private Sub Cargarlineaopcional2_()
        Dim tmp_obj As Integer = 0
        tmp_obj = cbo_idlinea2.EditValue

        Dim linea_ As New cls_linea_
        linea_.CargarXidobra(obj_obra.idcentrocostoorden)
        For Each linea As cls_linea_ In linea_.Items
            If linea.idlinea = obj_linea.idlinea Then
                linea_.Items.Remove(linea)
                Exit For
            End If
        Next
        If linea_.Items.Count = 0 Then linea_.Items = Nothing
        cargarCombo(cbo_idlinea2, linea_.Items, False, True)
        cbo_idlinea2.EditValue = IIf(tmp_obj <> 0, tmp_obj, cbo_idlinea2.EditValue)
    End Sub
    Private Sub Cargarproveedor_()
        Dim tmp_obj As Integer = 0
        tmp_obj = cbo_proveedor.EditValue

        Dim proveedor_ As New cls_proveedor_
        proveedor_.CargarTodos(True)
        cargarCombo(cbo_proveedor, proveedor_.Items, False, True)
        cbo_proveedor.EditValue = IIf(tmp_obj <> 0, tmp_obj, cbo_proveedor.EditValue)
    End Sub

    Private Sub Cargarplano_()
        Dim tmp_obj As Integer = 0
        tmp_obj = cbo_plano.EditValue

        Dim plano_ As New cls_plano_
        plano_.CargarXidobra(obj_obra.idcentrocostoorden)
        cargarCombo(cbo_plano, plano_.Items, False, True)
        cbo_plano.EditValue = IIf(tmp_obj <> 0, tmp_obj, cbo_plano.EditValue)
    End Sub

    Private Sub Cargararea_()
        Dim tmp_obj As Integer = 0
        tmp_obj = cbo_area.EditValue

        Dim area_ As New cls_area_
        area_.CargarXidobra(obj_obra.idcentrocostoorden)
        cargarCombo(cbo_area, area_.Items, False, True)
        cbo_area.EditValue = IIf(tmp_obj <> 0, tmp_obj, cbo_area.EditValue)
    End Sub

    Private Sub Cargarskid_()
        Dim tmp_obj As Integer = 0
        tmp_obj = cbo_skid.EditValue

        Dim skid_ As New cls_skid_
        skid_.CargarXidobra(obj_obra.idcentrocostoorden)
        cargarCombo(cbo_skid, skid_.Items, False, True)
        cbo_skid.EditValue = IIf(tmp_obj <> 0, tmp_obj, cbo_skid.EditValue)
    End Sub

    Public Shadows Sub Nuevo(ByRef listado As Form)
        Try
            frm_listado = listado
            'Me.Text = CType(frm_listado, xtf_ListadoBase).lbl_modulo.Text & " - Nuevo"
            Me.Text = "Isométrico - Nuevo"
            obj_isometrico_ = New cls_isometrico_
            btn_Registro.Enabled = False
            Me.Show()
            txt_codigo.Focus()
        Catch ex As Exception
            'Dim frmError As New frm_ErrorManager(ex.Message)
        End Try
    End Sub

    Public Shadows Sub Modificar(ByRef isometrico As cls_isometrico_, ByRef listado As Form, ByRef row As DataRow)
        If IsNothing(obj_isometrico_) Then
            obj_isometrico_ = New cls_isometrico_
        End If
        obj_isometrico_ = isometrico
        frm_listado = listado
        ulg_row = row : bbi_imprimir.Enabled = True

        'Me.Text = CType(frm_listado, xtf_ListadoBase).lbl_modulo.Text & " - " & obj_isometrico_.ToString
        Me.Text = "Isométrico - " & obj_isometrico_.ToString
        txt_ID.Text = obj_isometrico_.idisometrico
        txt_Nombre.Text = obj_isometrico_.nombre
        txt_codigo.Text = obj_isometrico_.codigo
        txt_hojas.Text = obj_isometrico_.hoja
        txt_hojastotal.Text = obj_isometrico_.hojatotal
        cbo_plano.EditValue = obj_isometrico_.idplano
        cbo_area.EditValue = obj_isometrico_.idarea
        cbo_skid.EditValue = obj_isometrico_.idskid
        cbo_idlinea1.EditValue = obj_isometrico_.idlineaadicional1
        cbo_idlinea2.EditValue = obj_isometrico_.idlineaadicional2
        cbo_proveedor.EditValue = obj_isometrico_.idproveedor
        txt_kiloscampo.Text = obj_isometrico_.kiloscampo
        txt_kilostaller.Text = obj_isometrico_.kilostaller
        txt_observaciones.Text = obj_isometrico_.observaciones


        btn_Registro.Enabled = True

        Me.Show()

    End Sub

    Public Overrides Function ComprobarDatos() As Boolean
        If Not ControlarValidaciones() Then
            ErrManagerShow("000004", Me)
            Return False
        End If

        If txt_nombre.Text = "" Then
            ErrManagerShow("001039", Me)
            Return False
        End If

        If txt_codigo.Text = "" Then
            ErrManagerShow("001040", Me)
            Return False
        End If

        Dim sum As Integer = 0
        For Each Chr As Char In txt_codigo.Text
            If IsNumeric(Chr) Then
                ErrManagerShow("001046", Me)
                Return False
            End If
            sum += 1
            If sum = 3 Then Exit For
        Next
        If sum < 3 Then
            ErrManagerShow("001046", Me)
            Return False
        End If

        'REGISTROS DUPLICADOS----------------------------------------------------
        Dim col As New Collections.Generic.List(Of stc_campoValor)
        Dim comp1 As New stc_campoValor
        comp1.campo = "nombre"
        comp1.valor = txt_nombre.Text
        col.Add(comp1)
        If ComprobarDuplicado("isometrico_", col, "idisometrico <> " & IIf(txt_ID.Text = "", 0, txt_ID.Text)) Then
            ErrManagerShow("000010", Me, " Dato: Nombre")
            Return False
        End If

        col.Clear()
        comp1.campo = "codigo"
        comp1.valor = txt_codigo.Text

        Dim comp2 As New stc_campoValor
        comp2.campo = "idobra"
        comp2.valor = obj_obra.idcentrocostoorden
        col.Add(comp2)

        col.Add(comp1)
        If ComprobarDuplicado("isometrico_", col, "idisometrico <> " & IIf(txt_ID.Text = "", 0, txt_ID.Text)) Then
            ErrManagerShow("000010", Me, " Dato: Código")
            Return False
        End If
        'REGISTROS DUPLICADOS----------------------------------------------------

        Return True
    End Function

    Public Shadows Sub btn_Guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Guardar.Click
        If ComprobarDatos() Then
            obj_isometrico_.nombre = txt_nombre.Text
            obj_isometrico_.codigo = txt_codigo.Text
            obj_isometrico_.hoja = txt_hojas.Text
            obj_isometrico_.hojatotal = txt_hojastotal.Text
            obj_isometrico_.idplano = cbo_plano.EditValue
            obj_isometrico_.idarea = cbo_area.EditValue
            obj_isometrico_.idskid = cbo_skid.EditValue
            obj_isometrico_.idlineaadicional1 = cbo_idlinea1.EditValue
            obj_isometrico_.idlineaadicional2 = cbo_idlinea2.EditValue
            obj_isometrico_.idproveedor = cbo_proveedor.EditValue
            obj_isometrico_.kiloscampo = txt_kiloscampo.Text
            obj_isometrico_.kilostaller = txt_kilostaller.Text
            obj_isometrico_.observaciones = txt_observaciones.Text
            obj_isometrico_.idobra = obj_obra.idcentrocostoorden
            obj_isometrico_.idlinea = obj_linea.idlinea

            If txt_ID.Text = "" Then
                obj_isometrico_.Nuevo(True)
                If frm_listado.Visible Then
                    ulg_row = CType(frm_listado, xtf_ABMsoldadura_).AgregarItem(obj_isometrico_)
                End If
                Modificar(obj_isometrico_, frm_listado, ulg_row)
            Else
                obj_isometrico_.Modificar()
                If frm_listado.Visible Then
                    CType(frm_listado, xtf_ABMsoldadura_).ModificarItem(obj_isometrico_, ulg_row)
                End If
                Modificar(obj_isometrico_, frm_listado, ulg_row)
            End If
            flg_Modificado = False : btn_Guardar.Enabled = False : bbi_guardar.Enabled = False
        End If
    End Sub

    Private Sub btn_Registro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Registro.Click
        If Not IsNothing(obj_isometrico_.registro) Then
            Dim ABMRegistro As New xtf_ABMRegistro(obj_isometrico_.registro)
            If ABMRegistro.ShowDialog() = Windows.Forms.DialogResult.OK Then
                flg_Modificado = True
            End If
        End If
    End Sub


    Private Sub btn_Cerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cerrar.Click
        If flg_Modificado Then
            bol_comprobarCerrado = False
            Select Case ErrManagerShow("000001", Me)
                Case Windows.Forms.DialogResult.Yes
                    If ComprobarDatos() Then
                        btn_Guardar_Click(Nothing, Nothing)
                    Else
                        Exit Sub
                    End If
                Case Windows.Forms.DialogResult.Cancel : bol_comprobarCerrado = True
                    Exit Sub
            End Select
        End If
        Me.Dispose()
    End Sub

End Class